﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Zeus.Components.Nopi.Excel;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;

using Zeus.Biz.Northwind.Model;

namespace Zeus.Thunder.Web.DataExporter
{
    public class SampleExcelWriter : NopiExcelWriter
    {
        private TemplateSetting _templateSetting;
        
        public SampleExcelWriter(string sheetName) : base(sheetName)
        {
            _templateSetting = SettingBuilder.Build();
        }
        
        protected override void FormatSheet()
        {
            int currentRowNum = 0;
            CellStyle cellStyle = HssfWorkbook.CreateCellStyle();
            Font font = HssfWorkbook.CreateFont();

            // Default font for exported xls            
            font.FontName = "Calibri";   // ExcelLocalResources.CellFontName;

            // initialize sheet format
            InitSheet();

            // write first empty row
            ExcelFormatHelper.WriteEmptyRow(Sheet, ref currentRowNum, true);

            // writer title
            // LocalRes.ExportToExcel.TitleOfPeriod
            ExcelFormatHelper.WriterTitle(Sheet, ref currentRowNum, 1, 
                "ICIS Dashboard Custom Date Range Download", font, cellStyle, _templateSetting);

            // write empty row
            ExcelFormatHelper.WriteEmptyRow(Sheet, ref currentRowNum, false);
            
            // write header
            Dictionary<string, string> headerDict = prepareHeaderData();
            ExcelFormatHelper.WriterHeader(Sheet, ref currentRowNum, 1, headerDict);
            
            // write empty row
            ExcelFormatHelper.WriteEmptyRow(Sheet, ref currentRowNum, false);

            // write body
            WriteBody(ref currentRowNum);

            // write empty row
            ExcelFormatHelper.WriteEmptyRow(Sheet, ref currentRowNum, false);

            // writer footer
            List<string> footList = prepareFooterData();
            ExcelFormatHelper.WriterFooter(Sheet, ref currentRowNum, 1, footList);
        }

        private void InitSheet()
        {
            // Hide grid lines
            Sheet.DisplayGridlines = false;

            // Set width for Columns
            Sheet.SetColumnWidth(0, _templateSetting.BlankColumnWidth * _templateSetting.ColumnWidthMultiplier);
        }

        private void WriteBody(ref int currentRowNum)
        {
            // prepare data
            List<Employee> employees = prepareTableHeadData();
            Dictionary<string, string> dict = prepareTableBodyData();
            
            // format data
            GenericExcelFormater<Employee>.WriteSingleTable(HssfWorkbook, Sheet, ref currentRowNum, 1, employees, dict, _templateSetting);
        }

        private Dictionary<string, string> prepareHeaderData()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            // ExcelLocalResources.Generated
            dict.Add("Generated", DateTime.Now.ToString("dd MMM yyyy HH:mm:ss"));
            dict.Add("Email", "davidgjy@163.com");

            return dict;
        }

        private List<Employee> prepareTableHeadData()
        {
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { EmployeeID = 1, LastName = "Michael", FirstName = "Jordan" });
            employees.Add(new Employee { EmployeeID = 2, LastName = "Kobe", FirstName = "Bryant" });
            employees.Add(new Employee { EmployeeID = 3, LastName = "Allen", FirstName = "Iverson" });
            employees.Add(new Employee { EmployeeID = 4, LastName = "Lebron", FirstName = "James" });
            employees.Add(new Employee { EmployeeID = 5, LastName = "Kyrie", FirstName = "Irving" });

            return employees;
        }

        private Dictionary<string, string> prepareTableBodyData()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("EmployeeID", "员工编号");
            dict.Add("FirstName", "姓");
            dict.Add("LastName", "名");

            return dict;
        }

        private List<string> prepareFooterData()
        {
            List<string> footList = new List<string>();
            footList.Add("Gerneral Template");
            footList.Add("Author: Master HaKu");
            string copyRight = string.Format("Copy Right: {0}", DateTime.Now.ToString("dd MMM yyyy"));
            footList.Add(copyRight);

            return footList;
        }
    }
}